#include <Functions.h>
#include <QStringList>
#include <QUrl>
#include <math.h>
#include <QApplication>
#include <iostream>
QString EEditorFunctions::QVariantToQString(const QVariant &variant)
{
    QString str = variant.toString();
    if(str == "")
    {
        switch (variant.type()) {
        case QVariant::Type::Matrix4x4:
            QMatrix4x4 matrix = variant.value<QMatrix4x4>();
            str = QMatrix4x4ToQString(matrix);
            break;

        }
    }
    return str;
}

QVariant EEditorFunctions::QStringToQVariant(QVariant::Type type, const QString &str)
{
    QVariant v;

    if(type == QVariant::Type::String)
    {
        v = str;
    }
    else if(type == QVariant::Type::Bool)
    {
        v = QStringToQVariantBool(str);
    }
    else if(type == QVariant::Type::Url)
    {
         v = QStringToQUrl(str);
    }
    else if(type == QVariant::Type::Matrix4x4)
    {
        v = QStringToQMatrix(str);
    }
    else if(type == QVariant::Type::Double)
    {
        v = QVariant(str.toDouble());
    }
    else
    {
        v = QVariant(str.toInt());
    }
    return v;

}

QString EEditorFunctions::QMatrix4x4ToQString(const QMatrix4x4 &matrix)
{
    QString str = QString("%1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12").
            arg(matrix(0,2)).
            arg(matrix(1,2)).
            arg(matrix(2,2)).
            arg(matrix(3,2)).
            arg(matrix(0,3)).
            arg(matrix(1,3)).
            arg(matrix(2,3)).
            arg(matrix(3,3)).
            arg(matrix(0,1)).
            arg(matrix(1,1)).
            arg(matrix(2,1)).
            arg(matrix(3,1));
    return str;
}

QMatrix4x4 EEditorFunctions::QStringToQMatrix(const QString &str)
{
    QMatrix4x4 matrix;
      QStringList strList = str.split(" ");

    if(strList.size() >= 8 )
    {
        matrix(0,2) = strList.at(0).toDouble();
        matrix(1,2) = strList.at(1).toDouble();
        matrix(2,2) = strList.at(2).toDouble();
        matrix(3,2) = strList.at(3).toDouble();
        matrix(0,3) = strList.at(4).toDouble();
        matrix(1,3) = strList.at(5).toDouble();
        matrix(2,3) = strList.at(6).toDouble();
        matrix(3,3) = 1.0;//strList.at(7).toDouble();
    }
    if(strList.size() >= 12)
    {
        matrix(0,1) = strList.at(8).toDouble();
        matrix(1,1) = strList.at(9).toDouble();
        matrix(2,1) = strList.at(10).toDouble();
        matrix(3,1) = 1.0;
    }
    return matrix;
}

QVariant EEditorFunctions::QStringToQVariantBool(const QString &str)
{
    QVariant v(QVariant::Type::Bool);
    v = false;
    if(str == "true")
    {
        v = true;
    }
    else if(str == "false")
    {
        v = false;
    }
    return v;
}

QVariant EEditorFunctions::QStringToQUrl(const QString &str)
{
    QUrl url = QUrl(str);
    return url;
}

bool EEditorFunctions::doubleEqual(double value1, double value2)
{
    if( abs( value2-value1)<0.000001 )
        return true;
    else
        return false;
}

void EEditorFunctions::postEvent(QObject *receiver, QEvent *event)
{
    QApplication::postEvent(receiver, event);
}

QEvent::Type EEditorFunctions::EObjectEventType()
{
    return (QEvent::Type)(QEvent::Type::User + 100);
}
